//
//  OADTwoCell.m
//  OASDK_Works
//
//  Created by ChenJie on 3/3/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "OADTwoCell.h"
#import "OASDK.h"

@interface OADTwoCell ()

@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UIView *view2;
@property (nonatomic, strong) UIView *view3;

@end

@implementation OADTwoCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.view1];
        [self.contentView addSubview:self.view2];
        [self.contentView addSubview:self.view3];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // calculate frame
    NSMutableDictionary *componentFrames = [NSMutableDictionary dictionary];
    CGFloat hMark = 0;
    CGFloat vMark = 0;
    for (OADLayout *layout in self.oad_layout.subLayouts) {
        NSString *componentName = layout.componentName;
        
        /*
        hMark += layout.marginLeft;
        vMark = layout.marginTop;
        */
        
        hMark = layout.marginLeft;
        vMark += layout.marginTop;
        
        CGRect componentFrame = CGRectMake(hMark, vMark, layout.width, layout.height);
        componentFrames[componentName] = [NSValue valueWithCGRect:componentFrame];
        
        NSLog(@"%s frame: %@", __PRETTY_FUNCTION__, NSStringFromCGRect(componentFrame));
        
        /*
        hMark += layout.width;
        hMark += layout.marginRight;
        */
        
        vMark += layout.height;
        vMark += layout.marginBottom;
    }
    
    
    
    // apply frame
    [componentFrames enumerateKeysAndObjectsUsingBlock:^(NSString *componentName, NSValue *frameValue, BOOL * _Nonnull stop) {
        UIView *component = [self valueForKey:componentName];
        if ([component isKindOfClass:UIView.class]) {
            component.frame = [frameValue CGRectValue];
//            NSLog(@"%@ frame is: %@", componentName, NSStringFromCGRect(component.frame));
        }
    }];
    
    [self.contentView oa_showBounds];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods



#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - <OADCellProtocol>

+ (OADLayout *)layoutWithViewModel:(OADTwoCellViewModel *)viewModel
                  staticCellHeight:(CGFloat)staticCellHeight {
    
    OADLayout *layout = [OADLayout new];
    
    //
    OADLayout *v1Layout = [OADLayout new];
    v1Layout.componentName = @"view1";
    v1Layout.marginLeft = 5;
    v1Layout.marginTop = 10;
    v1Layout.marginRight = 15;
    v1Layout.width = 23;
    v1Layout.height = 23;
    
    [layout addSubLayout:v1Layout];
    
    //
    OADLayout *v2Layout = [OADLayout new];
    v2Layout.componentName = @"view2";
    v2Layout.marginLeft = 10;
    v2Layout.marginRight = 5;
    v2Layout.marginTop = 5;
    v2Layout.width = 100;
    v2Layout.height = 44;
    
    [layout addSubLayout:v2Layout];
    
    //
    OADLayout *v3Layout = [OADLayout new];
    v3Layout.componentName = @"view3";
    v3Layout.marginLeft = 15;
    v3Layout.marginTop = 0;
    v3Layout.marginRight = 20;
    v3Layout.width = 30;
    v3Layout.height = 30;
    
    [layout addSubLayout:v3Layout];
    
    return layout;
}

+ (CGFloat)cellHeightLayoutsWithViewModel:(OADTwoCellViewModel *)viewModel
                                   layout:(OADLayout *)layout{    
    
    NSLog(@"%s: %@", __PRETTY_FUNCTION__, [layout layoutString]);
    
    // calculate frame
    NSMutableDictionary *componentFrames = [NSMutableDictionary dictionary];
    CGFloat hMark = 0;
    CGFloat vMark = 0;
    for (OADLayout *subLayout in layout.subLayouts) {
        NSString *componentName = subLayout.componentName;
        
        /*
        hMark += layout.marginLeft;
        vMark = layout.marginTop;
        */
        
        hMark = subLayout.marginLeft;
        vMark += subLayout.marginTop;
        
        CGRect componentFrame = CGRectMake(hMark, vMark, subLayout.width, subLayout.height);
        componentFrames[componentName] = [NSValue valueWithCGRect:componentFrame];
        
        NSLog(@"%s frame: %@", __PRETTY_FUNCTION__, NSStringFromCGRect(componentFrame));
        
        /*
        hMark += layout.width;
        hMark += layout.marginRight;
        */
        
        vMark += subLayout.height;
        vMark += subLayout.marginBottom;
    }
    
//    // apply frame
//    [componentFrames enumerateKeysAndObjectsUsingBlock:^(NSString *componentName, NSValue *frameValue, BOOL * _Nonnull stop) {
//        UIView *component = [self valueForKey:componentName];
//        if ([component isKindOfClass:UIView.class]) {
//            component.frame = [frameValue CGRectValue];
//            NSLog(@"%@ frame is: %@", componentName, NSStringFromCGRect(component.frame));
//        }
//    }];
    
    
    
    return vMark;
}

+ (void)layoutsWithViewModel:(OADTwoCellViewModel *)viewModel
            staticCellHeight:(CGFloat)staticCellHeight {
    
}

+ (OADLayout *)layoutWithViewModel:(id<OADCellViewModelProtocol>)viewModel {
    
    OADLayout *layout = [OADLayout new];
    
    //
    OADLayout *v1Layout = [OADLayout new];
    v1Layout.componentName = @"view1";
    v1Layout.marginLeft = arc4random() % 100;
    v1Layout.marginTop = 5;
    v1Layout.marginRight = arc4random() % 100;
    v1Layout.width = arc4random() % 100;
    v1Layout.height = arc4random() % 100;
    
    [layout addSubLayout:v1Layout];
    
    //
    OADLayout *v2Layout = [OADLayout new];
    v2Layout.componentName = @"view2";
    v2Layout.marginLeft = arc4random() % 100;
    v2Layout.marginRight = arc4random() % 100;
    v2Layout.marginTop = 5;
    v2Layout.width = arc4random() % 100;
    v2Layout.height = arc4random() % 100;
    
    [layout addSubLayout:v2Layout];
    
    //
    OADLayout *v3Layout = [OADLayout new];
    v3Layout.componentName = @"view3";
    v3Layout.marginLeft = arc4random() % 100;
    v3Layout.marginTop = 5;
    v3Layout.marginRight = arc4random() % 100;
    v3Layout.marginBottom = 5;
    v3Layout.width = arc4random() % 100;
    v3Layout.height = arc4random() % 100;
    
    [layout addSubLayout:v3Layout];
    
    NSLog(@"%s: %@", __PRETTY_FUNCTION__, [layout layoutString]);
    
    return layout;
}

#pragma mark - Setter



#pragma mark - Getter

- (UIView *)view1 {
    if (!_view1) {
        UIView *view = [UIView new];
        _view1 = view;
    }
    return _view1;
}

- (UIView *)view2 {
    if (!_view2) {
        UIView *view = [UIView new];
        _view2 = view;
    }
    return _view2;
}

- (UIView *)view3 {
    if (!_view3) {
        UIView *view = [UIView new];
        _view3 = view;
    }
    return _view3;
}

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
